package com.crm.dao;

import com.crm.model.PageBean;
import com.crm.model.business.Question;
import com.crm.util.HibernateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import java.math.BigInteger;
import java.util.List;

public class QuestionDao {

    /**
     * 根据id获取问题
     */
    public Question getQuestionById(String id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Question question = (Question) session.get(Question.class, Integer.parseInt(id));

        session.getTransaction().commit();
        return question;
    }

    /**
     * 分页查询问题结合
     */
    public List<Question> list(PageBean pageBean) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        String hql = "from Question";
        Query query = session.createQuery(hql);
        if (ObjectUtils.allNotNull(pageBean)) {
            query.setFirstResult(pageBean.getStart());
            query.setMaxResults(pageBean.getPageSize());
        }
        @SuppressWarnings("unchecked")
        List<Question> questionList = (List<Question>) query.list();

        session.getTransaction().commit();
        return questionList;
    }

    /**
     * 分页条件查询问题结合
     */
    public List<Question> list(Question question, PageBean pageBean) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        StringBuilder hql = new StringBuilder("from Question");
        if (ObjectUtils.allNotNull(question)) {
            if(ObjectUtils.isNotEmpty(question.getSubject())){
                hql.append(" and subject like '%").append(question.getSubject()).append("%'");
            }
        }
        Query query = session.createQuery(hql.toString().replaceFirst("and", "where"));
        if (ObjectUtils.allNotNull(pageBean)) {
            query.setFirstResult(pageBean.getStart());
            query.setMaxResults(pageBean.getPageSize());
        }
        @SuppressWarnings("unchecked")
        List<Question> questionList = (List<Question>) query.list();

        session.getTransaction().commit();
        return questionList;
    }

    /**
     *  统计总数
     */
    public int count() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        String sql = "select count(*) from eo_question";
        Query query = session.createSQLQuery(sql);
        int count = ((BigInteger) query.uniqueResult()).intValue();

        session.getTransaction().commit();
        return count;
    }

    /**
     *  按条件统计总数
     */
    public int count(Question question) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        StringBuilder sql = new StringBuilder("select count(*) from eo_question");
        if (question != null && StringUtils.isNotEmpty(question.getSubject())) {
            sql.append(" and subject like '%").append(question.getSubject()).append("%'");
        }
        Query query = session.createSQLQuery(sql.toString().replaceFirst("and", "where"));
        int count = ((BigInteger) query.uniqueResult()).intValue();

        session.getTransaction().commit();
        return count;
    }

    public void delete(Question question) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.delete(question);
        session.getTransaction().commit();
    }

    public void save(Question question) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.merge(question);
        session.getTransaction().commit();
    }
}
